using System;
using System.Collections.Generic;
using System.Text;
using System.IO;


namespace Spike.ContractBuilder
{
    [Serializable]
    public class TestCase
    {
        public event EventHandler OnUpdated;

        public void Save(string filename)
        {

            using (TextWriter writer = File.CreateText(filename))
            {
                writer.Write(SerializationHelper<TestCase>.SerializeServiceObject(this));
            }
        }

        public static TestCase Load(string filename)
        {
            TestCase test = null;
            using (TextReader reader = File.OpenText(filename))
            {
                test = SerializationHelper<TestCase>.Deserialize(reader.ReadToEnd());
            }

            return test;
        }

        public TestCase()
        {

        }

        public TestCase(string name, string contract, string endpoint, string assembly, string method, string url)
        {
            this.Name = name;
            this.ContractName = contract;
            this.Assembly = assembly;
            this.EndpointName = endpoint;
            this.Method = method;
            this.URL = url;

            this.Input = ContractGenerator.Instance.GenerateSampleInput(_assembly, _contractName, _endpointName, _method);
        }

        #region Properties
        private string _contractName;
        public string ContractName
        {
            get { return _contractName; }
            set
            {
                _contractName = value;
            }
        }

        private string _assembly;

        public string Assembly
        {
            get { return _assembly; }
            set { _assembly = value; }
        }

        private string _endpointName;

        public string EndpointName
        {
            get { return _endpointName; }
            set { _endpointName = value; }
        }

        private string _method;

        public string Method
        {
            get { return _method; }
            set { _method = value; }
        }


        string _name = string.Empty;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        string _input = string.Empty;
        public string Input
        {
            get { return _input; }
            set { _input = value; }
        }

        DateTime? _lastExecuted = null;
        public DateTime? LastExecuted
        {
            get { return _lastExecuted; }
            set
            {
                _lastExecuted = value;
                if (OnUpdated != null)
                    OnUpdated(this, EventArgs.Empty);
            }
        }
        private string _url;

        public string URL
        {
            get { return _url; }
            set { _url = value; }
        }
	
        #endregion

        /// <summary>
        /// verify the assembly referenced by name is still valid, and correct if it has changed
        /// </summary>
        /// <remarks>The assembly is rebuilt every time the metadata is retrieved from the wsdl</remarks>
        public void VerifyAssemblyReference()
        {
            if(!ContractGenerator.Instance.Contracts[ContractName].Endpoints[EndpointName].Assemblies.Contains(Assembly))
                Assembly = ContractGenerator.Instance.Contracts[ContractName].Endpoints[EndpointName].Assemblies[0];
        }
    }
}
